<?php 
/**
* ZhiPHP 值得买模式的海淘网站程序
* ====================================================================
* 版权所有 杭州言商网络有限公司，并保留所有权利。
* 网站地址: http://www.zhiphp.com
* 交流论坛: http://bbs.pinphp.com
* --------------------------------------------------------------------
* 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
* 使用；不允许对程序代码以任何形式任何目的的再发布。
* ====================================================================
* Author: brivio 
* 授权技术支持: 1142503300@qq.com
*/
class alipayAction extends frontendAction {


    public function index() {

		$order_id = $this->_get('order_id','intval');
		$info = D('order')->where(array('id'=>$order_id))->find();

		$price = $info['prices'];

		$orderid = date('Ymd').$order_id.rand('111','999');
		$host = 'http://'.$this->_server('HTTP_HOST'); //获取域名

		$url = $host.u('alipay/dopay');
		$url2 = $host.u('alipay/showpay');

		//创建并调用支付宝支付行为
		Vendor('alipay_v3.alipay');
		$alipay = new publicAlipayV3;
		$alipay->setConfig();
		$alipay->doPay($order_id,$orderid,$price,$url,$url2);
    }

	public function dopay() {
    	$db_pre = C('DB_PREFIX');
		//创建并调用支付宝支付行为
		Vendor('alipay_v3.alipay');
		$alipay = new publicAlipayV3;
		$alipay->setConfig();
		//$status = $alipay->doReturnWap();
		$status = $_POST;
		$order_id = $status['out_trade_no']; //本地订单号
		if($status != false && $order_id > 0){
			$order = D('order')->field('id,status')->where(' id = '.$order_id)->find();
			if($order['status'] == 0){
				$data['status']  =  1; //支付成功
				$data['pays_status']  =  1;
				$data['pays']  =  3;
				$data['pays_price']  =  $status['total_fee'];
				$data['pays_time']  =  date('Y-m-d H:i:s');
				$data['pays_data']  =  json_encode($status);
				$data['pays_sn']  =  $status['trade_no'];
				D('order')->where(array('id'=>$order_id))->save($data);
				/* 反馈操作 */
				$this->callbackOrder($order_id);
			}
		}
	}

	/* 特殊订单支付后进行反馈操作 */
	function callbackOrder($id){
		$order = D('order')->field('id,uid,uname,status,prices,type')->where(' id = '.$id)->find();
		if($order['status'] == 0) return false;
		switch ($order['type']){
			case 1:
				/* 充值 */
				D('user')->where(array('id'=>$order['uid']))->setInc('price',$order['prices']);
				$recharge_id = D('user_recharge')->where(array('order_id'=>$id))->getField('id');
				D('price_log')->add(array(
					'uid' => $order['uid'],
					'uname' => $order['uname'],
					'price' => $order['prices'],
					'action' => 'recharge',
					'add_time' => date('Y-m-d H:i:s'),
					'remark' => '在线充值',
					'key_id' => $recharge_id,
				));
				D('user_recharge')->where(array('order_id'=>$id))->save(array('status' => 1));
				break;
			default:
				return false;
		}
	}

    public function showpay() {
		if($_SESSION['ret_url'] != ''){
			redirect($_SESSION['ret_url']);
		}
		$info = D('order')->where(array('id'=>$_GET['out_trade_no']))->find();
		$this->assign('order', $info);
		if($info['pays_status'] == 1){
			$status = 1; //成功
		}else{
			$status = 0; //失败
		}
		$this->assign('status', $status);
		$this->display();
	}

}